package com.itheima.config;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(filterName = "encodingFilter", urlPatterns = "/*")
public class EncodingFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化代码（如果需要）
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 设置请求和响应的字符编码为 UTF-8
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");

        // 继续执行过滤链
        filterChain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {
        // 清理代码（如果需要）
    }
}